<?php

namespace App\Api;
use App\Domain\User\User as UserDomain;
use PhalApi\Api;


/**
 * 非常简单接口
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Fcjd extends Api
{

    public function getRules()
    {
        return array(
            // 单位管理设置
            'getUnit'       => array(
                'username'  => array('name' => 'username', 'require' => true, 'desc' => '用户名'),
            ),
            // 单位管理设置
            'getUnitWord'   => array(
                'unit'  => array('name' => 'unit', 'require' => true, 'desc' => '单位名'),
            ),
            'getPerson'       => array(
                'username'  => array('name' => 'username', 'require' => true, 'desc' => '用户名'),
                'page'  => array('name' => 'page', 'require' => true, 'desc' => '当前页'),
                'size'  => array('name' => 'size', 'require' => true, 'desc' => '每页显示多少'),
                'order' => array('name' => 'order', 'require' => false, 'desc' => '排序'),
                'where' => array('name' => 'where', 'require' => true, 'desc' => '查询条件'),
            ),
            'getListByUsername' => array(
                'username'  => array('name' => 'username', 'require' => true, 'desc' => '用户名'),
                'page'  => array('name' => 'page', 'require' => true, 'desc' => '当前页'),
                'size'  => array('name' => 'size', 'require' => true, 'desc' => '每页显示多少'),
                'order' => array('name' => 'order', 'require' => false, 'desc' => '排序'),
                'where' => array('name' => 'where', 'require' => true, 'desc' => '查询条件'),
            ),
        );
    }


    /**
     * 获取单位模板
     * @desc 更新个人信息
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function getUnitWord()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $unit = $this->unit;
        $ishave = \PhalApi\DI()->notorm->idcard_fcjd_unit->where('name', $unit)->fetchOne();
        if($ishave){
            return $ishave;
        }

    
    }

    /**
     * 用usrname获取信息
     * @desc 更新个人信息
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function getListByUsername()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $username = $this->username;
        $page = $this->page;
        $size = $this->size;
        $order = $this->order;
        $where = json_decode($this->where, true);

        // return $this;

        $domain = new UserDomain();
        if( $username ){
            $user = $domain->getUserByUsername($username, '*');
            if ($user) {
                $where['unit'] = $user['unit'];
                return \App\getInfoByPageByWhereByData(
                    array(
                    'page'=>$page,
                    'size'=>$size,
                    'order'=>$order,
                    'table'=>'idcard_check_list',
                    'where'=>json_encode($where),
                    )
                );
            }
        }
    }

    
    /**
     * 获取单位人员
     * @desc 更新个人信息
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function getPerson()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $username = $this->username;
        $page = $this->page;
        $size = $this->size;
        $order = $this->order;
        $where = json_decode($this->where, true);

        // return $this;

        $domain = new UserDomain();
        if( $username ){
            $user = $domain->getUserByUsername($username, '*');
            if ($user) {
                $where['unit'] = $user['unit'];
                return \App\getInfoByPageByWhereByData(
                    array(
                    'page'=>$page,
                    'size'=>$size,
                    'order'=>$order,
                    'table'=>'phalapi_user',
                    'where'=>json_encode($where),
                    )
                );
            }
        }
    }


    /**
     * 获取单位设置
     * @desc 更新个人信息
     * @return array data 签名url
     * @exception 400 非法请求，参数传递错误
     */
    public function getUnit()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $username = $this->username;

        $domain = new UserDomain();


        if( $username ){
            $user = $domain->getUserByUsername($username, '*');
            if ($user) {
                $ishave = \PhalApi\DI()->notorm->idcard_fcjd_unit->where('ids', $user['id'])->fetchOne();
                if ( $ishave ){
                    \PhalApi\DI()->notorm->idcard_fcjd_unit->where('ids', $user['id'])->update(
                        array(
                            'name' => $user['unit'],
                            'update_time' => date('Y-m-d H:i:s',time()),
                        )
                    );
                    return $ishave;
                }
                return \PhalApi\DI()->notorm->idcard_fcjd_unit->insert(array(
                    'ids' => $user['id'],
                    'name' => $user['unit'],
                    'update_time' => date('Y-m-d H:i:s',time()),
                    'create_time' => date('Y-m-d H:i:s',time()),
                ));
            }
        }
    }

}
